package com.bwie.init;

import com.bwie.common.pojo.po.GoodsPo;
import com.bwie.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class GoodsInit  implements ApplicationRunner {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        redisTemplate.delete("goods:list:msg");
        List<GoodsPo> list = goodsMapper.list(null);
        Map<String, List<GoodsPo>> map = list.stream().collect(Collectors.groupingBy(GoodsPo::getGoodsName));
        redisTemplate.opsForHash().putAll("goods:list:msg", map);
    }
}
